(use-modules
 ;; SRFI 64 for unit testing facilities
 (srfi srfi-64)
 ;; utils - the code to be tested
 (tree)
 (dataset))


(test-begin "tree-test")


(test-group
 "make-leaf-node"

 (test-equal "make-leaf-node-1"
   (make-leaf-node (list #(3.771244718 1.784783929 0)
                         #(2.728571309 1.169761413 0)
                         #(4.678319846 2.81281357 0)))
   (make-node (list #(3.771244718 1.784783929 0)
                    #(2.728571309 1.169761413 0)
                    #(4.678319846 2.81281357 0))
              'none
              'none
              empty-dataset
              empty-dataset)))


(test-group
 "make-leaf-node-from-split-node"
 (test-equal "make-leaf-node-from-split-node-1"
   (make-leaf-node-from-split-node
    (make-node (list #(2.3 1.0 4.0 0)
                     #(2.0 1.0 3.0 1)
                     #(2.3 1.0 3.0 1)
                     #(2.0 1.0 3.0 1)
                     #(2.4 1.0 3.0 1))
               2
               4.0
               (make-leaf-node (list #(2.0 1.0 3.0 1)
                                     #(2.3 1.0 3.0 1)
                                     #(2.0 1.0 3.0 1)
                                     #(2.4 1.0 3.0 1)))
               (make-leaf-node (list #(2.3 1.0 4.0 0)))))
   (make-leaf-node (list #(2.3 1.0 4.0 0)
                         #(2.0 1.0 3.0 1)
                         #(2.3 1.0 3.0 1)
                         #(2.0 1.0 3.0 1)
                         #(2.4 1.0 3.0 1)))))


(test-group
 "leaf-node?"

 (test-assert "leaf-node?-1"
   (leaf-node? (make-leaf-node (list #(5.0 6.0 0)
                                     #(7.0 8.0 1)))))


 (test-assert "leaf-node?-2"
   (not
    (leaf-node? (make-node (list #(5.0 6.0 0)
                                 #(7.0 8.0 1))
                           0
                           5.0
                           (make-leaf-node (list #(5.0 6.0 0)))
                           (make-leaf-node (list #(7.0 8.0 1)))))))

 (test-assert "leaf-node?-3"
   (let ([node (make-node (list #(3.771244718 1.784783929 0)
                                #(2.728571309 1.169761413 0)
                                #(4.678319846 2.81281357 1))
                          0
                          4.678319846
                          (make-leaf-node (list #(3.771244718 1.784783929 0)
                                                #(2.728571309 1.169761413 0)))
                          (make-leaf-node (list #(4.678319846 2.81281357 1))))])
     (and (leaf-node? (node-left node))
          (leaf-node? (node-right node))))))


(test-group
 "last-split-node?"

 (test-assert "last-split-node?-1"
   (not (last-split-node? (make-leaf-node (list #(3.771244718 1.784783929 0)
                                                #(2.728571309 1.169761413 0)
                                                #(4.678319846 2.81281357 1))))))
 (test-assert "last-split-node?-2"
   (last-split-node? (make-node (list #(3.771244718 1.784783929 0)
                                      #(2.728571309 1.169761413 0)
                                      #(4.678319846 2.81281357 1))
                                0
                                4.678319846
                                (make-leaf-node (list #(3.771244718 1.784783929 0)
                                                      #(2.728571309 1.169761413 0)))
                                (make-leaf-node (list #(4.678319846 2.81281357 1)))))))


(test-end "tree-test")
